from urllib import request
from lxml import etree

header_dict = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36",
}

def get_http(load_url, header):
    res = ""
    try:
        req = request.Request(url=load_url, headers=header)  # 创建请求对象
        coonect = request.urlopen(req)  # 打开该请求
        byte_res = coonect.read()  # 读取所有数据，很暴力
        try:
            res = byte_res.decode(encoding='utf-8')
        except:
            res = byte_res.decode(encoding='gbk')
    except Exception as e:
        print(e)
    return res
j = 0
url_list=["http://bjfx.gov.cn/fsfx"]
url = "http://bjfx.gov.cn/fsfx/"
page = get_http(url, header_dict)
print(page)
pages = etree.HTML(page)
imgs = pages.xpath("//img/@src")
texts = pages.xpath("//ul//li//span/text()")
urls = pages.xpath("//ul//a/@href")
print(urls)
print(texts)
print(imgs)
for img in imgs:
    request.urlretrieve(url+img, "f:/%s.jpg"%(j))
    j += 1